﻿using FarPoint.Web.Spread;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.ScrollTips
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (IsPostBack) return;
      InitSpread(this.FpSpread1);
      this.FpSpread1.UseClipboard = false;
    }

    private void InitSpread(FpSpread spread)
    {
      //set Properties
      SheetView sheet = spread.ActiveSheetView;
      sheet.AllowPage = false;
      sheet.ScrollingContentVisible = true;
      sheet.ScrollingContentColumns = "0,1,2";
      sheet.Columns.Count = 6;
      sheet.Rows.Count = 0;
      sheet.Columns[0].Width = 30;
      sheet.Columns[1].Width = 200;
      sheet.Columns[2].Width = 90;
      sheet.Columns[3].Width = 220;
      sheet.Columns[4].Width = 90;
      sheet.Columns[5].Width = 90;
      sheet.Columns[0].HorizontalAlign = HorizontalAlign.Center;
      sheet.Columns[4].CellType = new DateTimeCellType();
      sheet.Columns[5].CellType = new DateTimeCellType();
      sheet.ColumnHeader.Cells[0, 0].Value = "图标";
      sheet.ColumnHeader.Cells[0, 1].Value = "名称";
      sheet.ColumnHeader.Cells[0, 2].Value = "大小";
      sheet.ColumnHeader.Cells[0, 3].Value = "类型";
      sheet.ColumnHeader.Cells[0, 4].Value = "修改日期";
      sheet.ColumnHeader.Cells[0, 5].Value = "创建日期";

      //set data
      DirectoryInfo root = new DirectoryInfo(MapPath("~"));
      sheet.Rows.Count = root.GetDirectories().Length + root.GetFiles().Length;

      int rowIndex = sheet.Rows.Count - 1;
      foreach (DirectoryInfo folder in root.GetDirectories())
      {
        sheet.Cells[rowIndex, 0].CellType = new ImageCellType(GetIconPath("folder"));
        sheet.Cells[rowIndex, 1].Value = folder.Name;
        sheet.Cells[rowIndex, 2].Value = String.Format("{0} byte", GetDirectoryLength(folder));
        sheet.Cells[rowIndex, 3].Value = GetTypeDesc("folder");
        sheet.Cells[rowIndex, 4].Value = folder.LastWriteTime;
        sheet.Cells[rowIndex, 5].Value = folder.CreationTime;

        rowIndex--;
      }

      foreach (FileInfo file in root.GetFiles())
      {
        sheet.Cells[rowIndex, 0].CellType = new ImageCellType(GetIconPath(file.Extension.TrimStart(".".ToCharArray())));
        sheet.Cells[rowIndex, 1].Value = file.Name;
        sheet.Cells[rowIndex, 2].Value = String.Format("{0} byte", file.Length);
        sheet.Cells[rowIndex, 3].Value = GetTypeDesc(file.Extension.TrimStart(".".ToCharArray()));
        sheet.Cells[rowIndex, 4].Value = file.LastWriteTime;
        sheet.Cells[rowIndex, 5].Value = file.CreationTime;

        rowIndex--;
      }
    }

    private string GetIconPath(string ext)
    {
      string p = IMAGE_FOLDER + "/icon.{0}.png";
      switch (ext.ToLowerInvariant())
      {
        case "master":
        case "aspx":
          p = String.Format(p, "aspx");
          break;
        case "css":
        case "folder":
        case "sln":
        case "csproj":
        case "cs":
        case "xml":
        case "config":
          p = String.Format(p, ext);
          break;
        case "user":
        case "suo":
          p = String.Format(p, "user");
          break;
        default:
          return p = String.Format(p, "default");
      }
      return p;
    }

    private string GetTypeDesc(string ext)
    {
      switch (ext.ToLowerInvariant())
      {
        case "master":
          return "ASP.NET Master Page";
        case "aspx":
          return "ASP.NET Server Page";
        case "css":
          return "Cascading Style Sheet Document";
        case "folder":
          return "File Folder";
        case "sln":
          return "Microsoft Visual Studio Solution";
        case "csproj":
          return "Visual C# Project file";
        case "cs":
          return "Visual C# Source file";
        case "user":
          return "Visual Studio Project User Options file";
        case "suo":
          return "Visual Studio Solution User Options";
        case "config":
          return "XML Configuration File";
        default:
          return "";
      }
    }

    public long GetDirectoryLength(DirectoryInfo di)
    {
      long len = 0;
      foreach (FileInfo fi in di.GetFiles())
      {
        len += fi.Length;
      }
      DirectoryInfo[] dis = di.GetDirectories();
      if (dis.Length > 0)
      {
        for (int i = 0; i < dis.Length; i++)
        {
          len += GetDirectoryLength(dis[i]);
        }
      }
      return len;
    }



  }
}